package com.anc.admin.demo;

import java.util.*;
import java.util.stream.Collectors;

public class TestLambda {

    public static void main(String[] args) {
        test1();
    }

    //count 统计
    public static void test7(){
        List<UserInfo> userInfoList = new ArrayList<>();
        userInfoList.add(new UserInfo(1L, "芋道源码", 18,"北京"));
        userInfoList.add(new UserInfo(3L, "打代码的芋艿", 26,"北京"));
        userInfoList.add(new UserInfo(2L, "程序员芋艿", 27,"北京"));

        long count = userInfoList.stream().filter(user -> user.getAge() > 18).count();
        System.out.println("大于18岁的用户:" + count);
    }
    //Max，Min 最大最小
    public static void test6(){
        List<UserInfo> userInfoList = new ArrayList<>();
        userInfoList.add(new UserInfo(1L, "芋道源码", 18,"北京"));
        userInfoList.add(new UserInfo(3L, "打代码的芋艿", 26,"北京"));
        userInfoList.add(new UserInfo(2L, "程序员芋艿", 27,"北京"));

        Optional<UserInfo> maxAgeUserInfoOpt = userInfoList.stream().max(Comparator.comparing(UserInfo::getAge));
        maxAgeUserInfoOpt.ifPresent(userInfo -> System.out.println("max age user:" + userInfo));

        Optional<UserInfo> minAgeUserInfoOpt = userInfoList.stream().min(Comparator.comparing(UserInfo::getAge));
        minAgeUserInfoOpt.ifPresent(userInfo -> System.out.println("min age user:" + userInfo));
    }
    //map 转换 转换大小写
    public static void test5(){
        List<String> list = Arrays.asList("jay", "tianluo");
        //转化为大写
        List<String> upperCaselist = list.stream().map(String::toUpperCase).collect(Collectors.toList());
        upperCaselist.forEach(System.out::println);
    }
    //排序
    public static void test4(){
        List<UserInfo> userInfoList = new ArrayList<>();
        userInfoList.add(new UserInfo(1L, "芋道源码", 18,"北京"));
        userInfoList.add(new UserInfo(3L, "打代码的芋艿", 26,"北京"));
        userInfoList.add(new UserInfo(2L, "程序员芋艿", 27,"北京"));

        /**
         *  sorted + Comparator.comparing 排序列表，
         */
        userInfoList = userInfoList.stream().sorted(Comparator.comparing(UserInfo::getAge)).collect(Collectors.toList());
        userInfoList.forEach(a -> System.out.println(a.toString()));

        System.out.println("开始降序排序");

        /**
         * 如果想降序排序，则可以使用加reversed()
         */
        userInfoList = userInfoList.stream().sorted(Comparator.comparing(UserInfo::getAge).reversed()).collect(Collectors.toList());
        userInfoList.forEach(a -> System.out.println(a.toString()));


    }
    //groupingBy 分组
    public static void test3(){
        List<UserInfo> originUserInfoList = new ArrayList<>();
        originUserInfoList.add(new UserInfo(1L, "芋道源码", 18,"深圳"));

        originUserInfoList.add(new UserInfo(3L, "打代码的芋艿", 26,"湛江"));
        originUserInfoList.add(new UserInfo(2L, "程序员芋艿", 27,"深圳"));
        Map<String, List<UserInfo>> result = new HashMap<>();
        for (UserInfo userInfo : originUserInfoList) {
            String city = userInfo.getCity();
            List<UserInfo> userInfos = result.get(city);
            if (userInfos == null) {
                userInfos = new ArrayList<>();
                result.put(city, userInfos);
            }
            userInfos.add(userInfo);
        }
        Map<String, List<UserInfo>> result2 = originUserInfoList.stream()
                .collect(Collectors.groupingBy(UserInfo::getCity));
    }
    // filter（）过滤
    public static void test2(){
        List<UserInfo> userInfoList = new ArrayList<>();
        userInfoList.add(new UserInfo(1L, "芋道源码", 18,"北京"));
        userInfoList.add(new UserInfo(2L, "程序员芋艿", 27,"北京"));
        userInfoList.add(new UserInfo(3L, "打代码的芋艿", 26,"北京"));

        /**
         * filter 过滤，留下超过18岁的用户
         */
        List<UserInfo> userInfoResultList = userInfoList.stream().filter(user -> user.getAge() > 18).collect(Collectors.toList());
        userInfoResultList.forEach(a -> System.out.println(a.getName()));
    }
    //list 转 map
    public static void test1(){
        List<UserInfo> userInfoList = new ArrayList<>();
        userInfoList.add(new UserInfo(1L, "芋道源码", 18,"北京"));
        userInfoList.add(new UserInfo(2L, "程序员芋艿", 27,"北京"));
        userInfoList.add(new UserInfo(2L, "打代码的芋艿", 26,"北京"));
        /**
         *  list 转 map
         *  使用Collectors.toMap的时候，如果有可以重复会报错，所以需要加(k1, k2) -> k1
         *  (k1, k2) -> k1 表示，如果有重复的key,则保留第一个，舍弃第二个
         */
        Map<Long, UserInfo> userInfoMap = userInfoList.stream().collect(Collectors.toMap(UserInfo::getId, userInfo -> userInfo, (k1, k2) -> k1));
        Map<Long, UserInfo> userInfoMap2 = userInfoList.stream().collect(Collectors.toMap(UserInfo::getId, userInfo -> userInfo, (k1, k2) -> k1));
        Map<Long, String> userNameInfoMap = userInfoList.stream().collect(Collectors.toMap(UserInfo::getId, UserInfo::getName, (value1, value2 )-> value2));
        //userInfoMap.values().forEach(a->System.out.println(a.getName()));
        System.out.println(userInfoMap2);
    }

































    static class UserInfo{
        private Long id;
        private  String name;
        private Integer age;
        private String city;

        public Long getId() {
            return id;
        }

        public void setId(Long id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        public String getCity() {
            return city;
        }

        public void setCity(String city) {
            this.city = city;
        }

        public UserInfo(Long id, String name, Integer age, String city) {
            this.id = id;
            this.name = name;
            this.age = age;
            this.city = city;
        }
    }



}
